/**
 * DWR proxy for ext return list or object
 *
 * @param dwrCall {String} DWR remote service method call.
 * @param params  {Array} DWR remote call base params
 *
 * @author liuhz
 */
CQCIS.data.DWRProxy = function(config){
    /**
     * @cfg call    load Call
     * @cfg save    save Call
     * @cfg update  update Call
     * @cfg params  params
     */
    this.call = config.call;
    this.dosave = config.save;
    this.doupdate = config.update;
    this.params = config.params;
    this.ownerCt = config.ownerCt;
    
    this.addEvents({
        beforesave: true,
        save: true,
        saveexception: true,
        beforeupdate: true,
        update: true,
        updateexception: true
    });
    
    CQCIS.data.DWRProxy.superclass.constructor.call(this);
};

Ext.extend(CQCIS.data.DWRProxy, Ext.data.DataProxy, {
    load: function(params, reader, callback, scope, arg){
        if (this.fireEvent("beforeload", this, params) !== false) {
            var delegate = this.loadResponse.createDelegate(this, [reader, callback, scope, arg], 1);
            var callParams = new Array();
            if (params.filter) {
                callParams.push(params.filter);
            }
            if (arg && arg.arg) {
                if (arg.arg instanceof Array) {
                    callParams = arg.arg.slice();
                } else {
                    callParams.push(arg.arg);
                }
            } else if (params && params.query) {
                var paramsObj = {
                    query: params.query
                }
                for (var obj in this.params) {
                    paramsObj[obj] = this.params[obj];
                }
                callParams.push(paramsObj);
            } else if (this.params) {
                if (this.params instanceof Array) {
                    callParams = this.params.slice();
                } else {
                    callParams.push(this.params);
                }
            }
            if (this.ownerCt) {
                var ownerCt = this.ownerCt;
                delegate = {
                    callback: delegate,
                    errorHandler: function(msg, ex){
                        if (ownerCt && ownerCt.loadMask) {
                            ownerCt.loadMask.hide();
                        }
                        if (ownerCt.getEl() && ownerCt.getEl().isMasked()) {
                            ownerCt.getEl().unmask();
                        }
                        if (ownerCt.body && ownerCt.body.isMasked()) {
                            ownerCt.body.unmask();
                        }
                        if (msg && msg.indexOf("登录系统") >= 0) {
                            CQCIS.alert(msg, function(){
                                if (top.userName) {
                                    top.logionWin.init();
                                } else {
                                    top.location.replace(CQCIS.ctxPath + 'index.html');
                                }
                            });
                        } else if (msg) {
                            CQCIS.msg(msg, '远程调用失败');
                            CQCIS.console(msg, 3);
                            if (ex.getDetailedMessage) {
                                CQCIS.console(ex.getDetailedMessage(), 3);
                            } else {
                                CQCIS.console(ex, 3);
                            }
                        }
                    }
                };
            }
            callParams.push(delegate);
            this.call.apply(this, callParams);
        } else {
            callback.call(scope || this, null, arg, false);
        }
    },
    
    loadResponse: function(response, reader, callback, scope, arg){
        var result;
        try {
            result = reader.read(response);
        } catch (e) {
            this.fireEvent("loadexception", this, null, response, e);
            callback.call(scope, null, arg, false);
            return;
        }
        
        callback.call(scope, result, arg, true);
    },
    
    save: function(params, callback, scope, arg){
        if (this.fireEvent("beforesave", this, params) !== false) {
            var delegate = this.saveResponse.createDelegate(this, [callback, scope, arg], 1);
            var callParams = new Array();
            if (params.filter) {
                callParams.push(params.filter);
            }
            if (arg && arg.arg) {
                if (arg.arg instanceof Array) {
                    callParams = arg.arg.slice();
                } else {
                    callParams.push(arg.arg);
                }
            } else if (this.params) {
                if (this.params instanceof Array) {
                    callParams = this.params.slice();
                } else {
                    callParams.push(this.params);
                }
            }
            callParams.push(delegate);
            this.dosave.apply(this, callParams);
        } else {
            callback.call(scope || this, null, arg, false);
        }
    },
    
    saveResponse: function(response, callback, scope, arg){
        callback.call(scope, response, arg, true);
    },
    
    update: function(params, callback, scope, arg){
        if (this.fireEvent("beforeupdate", this, params) !== false) {
            var delegate = this.updateResponse.createDelegate(this, [callback, scope, arg], 1);
            var callParams = new Array();
            if (params.filter) {
                callParams.push(params.filter);
            }
            if (arg && arg.arg) {
                if (arg.arg instanceof Array) {
                    callParams = arg.arg.slice();
                } else {
                    callParams.push(arg.arg);
                }
            } else if (this.params) {
                if (this.params instanceof Array) {
                    callParams = this.params.slice();
                } else {
                    callParams.push(this.params);
                }
            }
            callParams.push(delegate);
            this.doupdate.apply(this, callParams);
        } else {
            callback.call(scope || this, null, arg, false);
        }
    },
    
    updateResponse: function(response, callback, scope, arg){
        callback.call(scope, response, arg, true);
    }
});
